/*
# Rouleur Log - A log for cyclists.
# Copyright (C) 2006  Edward Copony (ecopony@gmail.com)
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
package com.blazingredfireball.rouleurlog.business
{
	import mx.rpc.IResponder;
	import flash.events.SQLEvent;
	import flash.events.SQLErrorEvent
	import com.adobe.cairngorm.business.ServiceLocator;
	import com.blazingredfireball.rouleurlog.vo.RideVO;
	import com.blazingredfireball.rouleurlog.model.RouleurlogModelLocator;
	import flash.data.SQLStatement;
	import mx.controls.Alert;
	
	public class RideDelegate
	{
	  private var model:RouleurlogModelLocator = RouleurlogModelLocator.getInstance();
		private var responder : IResponder;
		
		public function RideDelegate(responder : IResponder)
		{
			this.responder = responder;
		}
		
		public function getRides():void
		{
		  var sql:String = "SELECT * FROM rides ORDER BY ride_on desc";
      model.doSql(sql, responder);
		}
		
		public function createRide(ride:Object):void
		{
		  var sql:String = "INSERT INTO rides (distance, comments, ride_on, bike_id, average_speed, " + 
		    "riding_time, route_id, max_speed, elevation_gain, created_on, updated_on) VALUES (" + 
        ride.distance + ", \"" + 
        ride.comments + "\", " + 
        ride.ride_on.getTime() + ", " + 
        ride.bike_id + ", " + 
        ride.average_speed + ", \"" + 
        ride.riding_time + "\", " + 
        ride.route_id + ", " + 
        ride.max_speed + ", " + 
        ride.elevation_gain + ", " + 
        ride.created_on.getTime() + ", " + 
        ride.updated_on.getTime() + ")";
      model.doSql(sql, responder);
		}
		
		public function deleteRide(id:int):void
		{
		  var sql:String = "DELETE FROM rides WHERE id = " + id
      model.doSql(sql, responder);
		}
		
		public function updateRide(id:int, ride:Object):void
		{
		  var sql:String = "UPDATE rides SET distance = \"" + ride.distance + "\", " + 
		      "comments = \"" + ride.comments + "\", " + 
		      "ride_on = " + ride.ride_on.getTime() + ", " + 
		      "bike_id = " + ride.bike_id + ", " + 
		      "average_speed = " + ride.average_speed + ", " + 
		      "riding_time = \"" + ride.riding_time + "\", " + 
		      "route_id = " + ride.route_id + ", " + 
		      "max_speed = " + ride.max_speed + ", " + 
		      "elevation_gain = " + ride.elevation_gain + ", " + 
		      "updated_on = " + new Date().getTime() + " " + 
		      "WHERE id = " + id;
      model.doSql(sql, responder);
		}
	}
}